package com.ja.openchat.viewmodels;

import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.ja.openchat.ui.me.SetPayPwdFragment;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResponse;
import com.zy.openchat.core.manager.TgResultBlock;

import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.Messenger;

import static com.blankj.utilcode.util.StringUtils.getString;


public class SetPayPwdOldViewModel extends BaseViewModel {

    public static final String TOKEN_SETPAYPWDOLDVIEWMODEL_FINISH = "token_setpaypwdoldviewmodel_finish";

    public SetPayPwdOldViewModel(@NonNull Application application) {
        super(application);
        Messenger.getDefault().register(this, TOKEN_SETPAYPWDOLDVIEWMODEL_FINISH, () -> finish());
    }

    //提交按钮的点击事件
    public BindingCommand confirmOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(SetPayPwdFragment.class.getCanonicalName());
        }
    });

    public void requestData(String text) {
        showProgress();
        TelegramManager.getTelegramManager().checkWalletPayPassword(getApplication(), text, new TgResultBlock<TgResponse>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                hideProgress();
                if (!obj.ok) {
                    showErrorTip(obj.errString);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("textOld", text);
                    startContainerActivity(SetPayPwdFragment.class.getCanonicalName(), bundle);
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                hideProgress();
                showErrorTip(getString(R.string.request_error));
            }
        });
    }
}
